package com.sec.kun.config;

/**
 * @Author zhoukun
 * @Date 2021/2/17 15:59
 */


import com.sec.kun.hancer.JwtTokenEnhancer;
import com.sec.kun.service.Impl.UserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;


import java.util.ArrayList;
import java.util.List;

/**
 * 授权服务器
 */
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    /**
     * 密码加密器
     */
    @Autowired
    private PasswordEncoder passwordEncoder;
    /**
     * 用户认证管理器
     */
    @Autowired
    private AuthenticationManager authenticationManager;
    /**
     * 用户服务
     */
    @Autowired
    private UserDetailsServiceImpl userDetailsServiceImpl;

    /**
     *Jwt配置类
     */
    @Autowired
    JwtAccessTokenConverter jwtAccessTokenConverter;

    @Autowired
    TokenStore tokenStore;
    /**
     * Jwt内容增强器
     */
    @Autowired
    JwtTokenEnhancer jwtTokenEnhancer;
    /**
     * 授权服务安全配置，主要用于放行客户端访问授权服务接口
     *
     * @param security AuthorizationServerSecurityConfigurer
     * @throws Exception 异常
     */
    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        //允许客户端表单提交
        security.allowFormAuthenticationForClients()
                //客户端校验token访问许可
                .checkTokenAccess("permitAll()")
                //客户端token调用许可
                .tokenKeyAccess("permitAll()");
    }
    /**
     * 客户端信息配置，可配置多个客户端，这里可以使用配置文件进行代替
     *
     * @param clients 客户端设置
     * @throws Exception 异常
     */

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()//内存中
                .withClient("client")//客户端ID
                .secret(passwordEncoder.encode("zk2000208"))//秘钥
                .accessTokenValiditySeconds(60)//设置jwt令牌有限期限（一分钟）
                .refreshTokenValiditySeconds(3600)//设置jwt令牌的有效期
                .redirectUris("https://www.bilibili.com")//重定向到的地址
                .scopes("all")//授权范围
                //支持 授权码、密码两种授权模式，支持刷新token功能
                .authorizedGrantTypes("authorization_code","password", "refresh_token")
                ;//授权类型为授权码模式
    }




    //密码模式需要配置
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints)
            throws Exception {
        //创建TokenEnhancerChain实例
        TokenEnhancerChain tokenEnhancerChain=new TokenEnhancerChain();
        List<TokenEnhancer> enhancerList=new ArrayList<>();
        //配置jtv内容增强器
        enhancerList.add(jwtTokenEnhancer);
        enhancerList.add(jwtAccessTokenConverter);
        tokenEnhancerChain.setTokenEnhancers(enhancerList);
        endpoints.authenticationManager(authenticationManager)
                .userDetailsService(userDetailsServiceImpl)
                .tokenStore(tokenStore)
                .accessTokenConverter(jwtAccessTokenConverter)
                .tokenEnhancer(tokenEnhancerChain)

        ;
    }




}
